/**
 * Program  : ParamUtil.java
 * Author   : qianj
 * Create   : 2012-3-22 下午05:14:28
 *
 * Copyright 2012 by newyulong Technologies Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of newyulong Technologies Ltd.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with newyulong Technologies Ltd.
 *
 */

package com.newyulong.implatform.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.SharedPreferences;

/**
 * 封装HTTP请求参数的UITL
 * 
 * @author qianj
 * @version 1.0.0
 * @2012-3-22 下午05:14:28
 */
public class ParamUtil {
	SharedPreferences sp;
	/**
	 * 返回http请求的必带的系统参数
	 * 
	 * @author qianj
	 * @create 2012-3-22 下午05:15:28
	 * @since
	 * @return
	 */
	private static Map<String, Object> getSystemParam() {
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("appkey", "android");
		map.put("v", "1.0");
		return map;
	}

	/**
	 * 将参数名升序排列，然后将参数名和参数值连接，组成字符串。
	 * 
	 * @author qianj
	 * @create 2012-3-23 上午10:35:26
	 * @since
	 * @param keys
	 * @param paramMap
	 * @return
	 */
	public static String getParamStr(Map<String, Object> paramMap) {
		
		//将参数中装入系统参数
		//paramMap.putAll(getSystemParam());
		StringBuffer buffer = new StringBuffer();
		ArrayList<String> keys = new ArrayList<String>();
		keys.addAll(paramMap.keySet());
		//将key排序
		ArrayList<String> keyList = sortStr(keys);
		
		//由排序后的key取出参数并组装参数字符串
		for (String key : keyList) {
			String param = paramMap.get(key).toString();
			buffer.append(key);
			buffer.append(param);
		}
		
		//密钥
		String securitykey = "android";
		
		return buffer.toString() + securitykey;
	}
	
	/**
	 * 得到平常http请求的参数（不用加密状态的）
	 * @author qianj
	 * @create 2012-3-26 下午03:11:43
	 * @since 
	 * @param paramMap
	 * @return
	 */
	public static String getParamNormol(Map<String, Object> paramMap) {
		
		//将参数中装入系统参数
		//paramMap.putAll(getSystemParam());
		StringBuffer buffer = new StringBuffer();
		ArrayList<String> keys = new ArrayList<String>();
		keys.addAll(paramMap.keySet());
		
		int count = keys.size();
		int i = 0;
		//由排序后的key取出参数并组装参数字符串
		for (String key : keys) {
			String param = paramMap.get(key).toString();
			buffer.append(key);
			buffer.append("=");
			buffer.append(param);
			i++;
			if(i < count){
				buffer.append("&");
			}
		}
		return buffer.toString();
	}

	/**
	 * 字符串排序
	 * 
	 * @author qianj
	 * @create 2012-3-23 上午10:45:22
	 * @since
	 * @param strList
	 * @return
	 */
	public static ArrayList<String> sortStr(ArrayList<String> strList) {
		int length = strList.size();
		String str1;
		String str2;
		int flag;
		// 冒泡排序
		for (int i = 0; i < length - 1; i++) {
			for (int j = 0; j < length - 1; j++) {
				str1 = strList.get(j);
				str2 = strList.get(j + 1);
				flag = str1.compareToIgnoreCase(str2);
				if (flag >= 0) {
					strList.set(j, str2);
					strList.set(j + 1, str1);
				}
			}
		}
		return strList;
	}

}
